package com.itcncp.wechat.app.mini.controller;
import com.itcncp.common.base.BaseController;
import com.itcncp.common.util.AESUtil;
import com.itcncp.wechat.app.common.constant.ApiResult;
import com.itcncp.wechat.app.common.constant.ApiResultConstant;
import com.itcncp.wechat.app.dao.model.UserCarInfo;
import com.itcncp.wechat.app.dao.model.UserCarInfoExample;
import com.itcncp.wechat.app.dao.model.UserCustomerInfo;
import com.itcncp.wechat.app.rpc.api.ItcsCarBrandInfoService;
import com.itcncp.wechat.app.rpc.api.UserCarInfoService;
import com.itcncp.wechat.app.rpc.api.UserCustomerInfoService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * Created by shuitcncp on 2016/12/10.
 */
@Controller
@RequestMapping("/plate")
@Api(tags = "车牌管理", description = "车牌的基本信息接口",
        produces = "produces",consumes = "consumes",basePath = "/plate")

public class UserCarController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger( UserCarController.class);

    @Autowired
    private UserCustomerInfoService userCustomerInfoService;
    @Autowired
    private UserCarInfoService userCarInfoService;
    @Autowired
    private ItcsCarBrandInfoService itcsCarBrandInfoService;

    @ResponseBody
    @ApiOperation(value="添加车牌",notes="添加车牌信息，行驶证图片需要先上传，之后将图片路径已URL的地址传入接口")
    @RequestMapping(value = "/addPlate", method = RequestMethod.POST)
    @ApiImplicitParams( value = {
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "Long", required = true, value = "用户ID"),
            @ApiImplicitParam(paramType = "query", name = "security", dataType = "String", required = true, value = "安全码"),
            @ApiImplicitParam(paramType = "query", name = "plate", dataType = "String", required = true, value = "车牌号"),
            @ApiImplicitParam(paramType = "query", name = "vehicleLicence", dataType = "String", required = true, value = "车辆行驶证图片地址")
    })
    public Object create(HttpServletRequest request,
                         @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
                         @RequestParam(required = true,defaultValue = "0",value = "security") String security,
                         @RequestParam(required = true,defaultValue = "",value = "plate") String plate,
                         @RequestParam(required = true,defaultValue = "",value = "vehicleLicence") String vehicleLicence ) {

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }
        //车牌验证
        if(StringUtils.isEmpty( plate )){
            return new ApiResult( ApiResultConstant.APP_PLATE_ERROR);
        }

        //车牌验证
        if(StringUtils.isEmpty( vehicleLicence )){
            return new ApiResult( ApiResultConstant.APP_PLATE_ERROR);
        }

        //判断添加车牌时输入的安全码是否正确
        //车牌验证
        if(StringUtils.isEmpty( security )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR);
        }

        UserCustomerInfo userInfo = userCustomerInfoService.selectByPrimaryKey( userId );
        if(null == userInfo){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR);
        }
        if(!userInfo.getSecurity().equals( AESUtil.encryptPassword( security ) )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR);
        }

        //验证添加车牌数量是否超出 5辆绑定数
//        UserCarInfoExample isBindCountExample = new UserCarInfoExample();
//        isBindCountExample.createCriteria().andDelEqualTo( new Byte( "2" ) ).andUserIdEqualTo(userId);
//        Integer isBindCount = userCarInfoService.countByExample( isBindCountExample );
//        if(isBindCount>=5){
//            return  new ApiResult( ApiResultConstant.APP_PLATE_OVERFLOW_COUNT );
//        }

        UserCarInfo userCarInfo = new UserCarInfo();
        userCarInfo.setUserId( userId );
        userCarInfo.setPlate( plate );
        userCarInfo.setVehicleLicence( vehicleLicence );

        userCarInfo = userCarInfoService.addUserCard( userCarInfo );

        if(null != userCarInfo){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return  new ApiResult( ApiResultConstant.APP_PLATE_EXIST );
    }


    @ResponseBody
    @ApiOperation(value="删除车牌",notes="需要在前端做一次确认提醒，是否确认删除车牌")
    @RequestMapping(value = "/delPlate", method = RequestMethod.POST)
    @ApiImplicitParams( value = {
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "Long", required = true, value = "用户ID"),
            @ApiImplicitParam(paramType = "query", name = "plate", dataType = "String", required = true, value = "车牌号")
    })
    public Object delPlate(HttpServletRequest request,
                         @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
                         @RequestParam(required = true,defaultValue = "",value = "plate") String plate) {

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.FAILED);
        }
        //车牌验证
        if(StringUtils.isEmpty( plate )){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        UserCarInfo userCarInfo = new UserCarInfo();
        userCarInfo.setUserId( userId );
        userCarInfo.setPlate( plate );
        userCarInfo.setDel( new Byte( "1" ) );

        Integer total = userCarInfoService.updateByPrimaryKeySelective( userCarInfo );

        if(total>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return  new ApiResult( ApiResultConstant.FAILED );
    }

    /**
     * 车牌列表
     * @return
     */
    @ResponseBody
    @ApiOperation(value="车牌列表",notes="一次性姜用户的车牌全部查出，包括未审核通过的车牌；车牌不可以修改，未审核通过的只能删除！")
    @RequestMapping(value = "/findPlate", method = RequestMethod.POST)
    @ApiImplicitParams( value = {
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "Long", required = true, value = "用户ID")
    })
    public Object findPlate(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        UserCarInfoExample userCarInfoExample = new UserCarInfoExample();
        UserCarInfoExample.Criteria criteria = userCarInfoExample.createCriteria();
        //数据是否删除  1、删除  2、不删除
        criteria.andDelEqualTo( new Byte( "2" ) );
        if(userId>0){
            criteria.andUserIdEqualTo( userId );
        }

        userCarInfoExample.setOrderByClause("plate desc");


        List<UserCarInfo> userCarInfoList = userCarInfoService.selectByExample(userCarInfoExample);

        return new ApiResult( ApiResultConstant.SUCCESS,userCarInfoList);
    }


    /**
     * 判斷用戶是否已經綁定車牌
     * @return
     */
    @ResponseBody
    @ApiOperation(value="检测用户是否已经绑定车牌",notes="每次登陆需检测用户是否已经绑定车牌，如未绑定需弹出提示 返回 0 未绑定车牌")
    @RequestMapping(value = "/isBindPlate", method = RequestMethod.POST)
    @ApiImplicitParams( value = {
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "Long", required = true, value = "用户ID")
    })
    public Object isBindPlate(HttpServletRequest request,
                            @RequestParam(required = false, defaultValue = "0", value = "userId") Long userId){

        //用户id验证
        if(userId<=0){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        UserCarInfoExample userCarInfoExample = new UserCarInfoExample();
        UserCarInfoExample.Criteria criteria = userCarInfoExample.createCriteria();
        //数据是否删除  1、删除  2、不删除
        criteria.andDelEqualTo( new Byte( "2" ) );
        //审核通过的车牌
        //默认审核状态 车牌审核 1、未审核 2、审核通过 3、审核未通过
        criteria.andIsCheckEqualTo(new Byte("2"));

        criteria.andUserIdEqualTo( userId );


        Integer isBindPlateCount = userCarInfoService.countByExample(userCarInfoExample);

        if(isBindPlateCount == 0){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        return new ApiResult( ApiResultConstant.SUCCESS);
    }

}